<?php
/**
 * Created by PhpStorm.
 * Script Name: MpFollow.php
 * Create: 2023/1/12 9:29
 * Description:
 * Author: fudaoji<fdj@kuryun.cn>
 */

namespace plugin\wechat\app\service;
use plugin\wechat\app\model\MpFollow as FollowM;

class MpFollow extends Mp
{
    /**
     * 获取最后一个粉丝
     * @param int $mp_id
     * @param int $company_id
     * @return array|mixed|\think\db\Query|\think\Model|null
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * Author: fudaoji<fdj@kuryun.cn>
     */
    static function getLastFollow($mp_id = 0, $company_id = 0){
        return FollowM::where('company_id', $company_id)
            ->where('mp_id', $mp_id)
            ->order('id', 'desc')
            ->find();
    }

    /**
     * 拉取粉丝
     * @param string $next_openid
     * @param null $mp_info
     * @return array|\EasyWeChat\Kernel\Support\Collection|object|\Psr\Http\Message\ResponseInterface|string
     * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
     * @throws \Psr\SimpleCache\InvalidArgumentException
     * Author: fudaoji<fdj@kuryun.cn>
     */
    static function userList($next_openid = '', $mp_info = null){
        $app = self::getApp($mp_info);
        $res = $app->user->list($next_openid);
        if(!empty($res['errcode'])){
            return $res['errmsg'];
        }
        return $res;
    }

    /**
     * 拉取用户信息列表
     * @param array $openid_list
     * @param null $mp_info
     * @return array|\EasyWeChat\Kernel\Support\Collection|object|\Psr\Http\Message\ResponseInterface|string
     * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
     * @throws \GuzzleHttp\Exception\GuzzleException
     * @throws \Psr\SimpleCache\InvalidArgumentException
     * Author: fudaoji<fdj@kuryun.cn>
     */
    static function userInfoList($openid_list = [], $mp_info = null){
        $app = self::getApp($mp_info);
        $res = $app->user->select($openid_list);
        if(!empty($res['errcode'])){
            return $res['errmsg'];
        }
        return $res;
    }

    /**
     * 插入数据
     * @param array $data
     * @return array
     * Author: fudaoji<fdj@kuryun.cn>
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    static function insertOne($data = []){
        !empty($data['tagid_list']) && $data['tagid_list'] = json_encode($data['tagid_list']);
        if(! $res = FollowM::where('company_id', $data['company_id'])->where('openid', $data['openid'])->find()){
            $res = (new FollowM())->createOne($data);
        }else if($data['tagid_list'] && empty($res['tagid_list'])){
            FollowM::update(['id' => $res['id'], 'tagid_list' => $data['tagid_list']]);
        }
        return $res;
    }

    /**
     * 为用户批量打标签
     * @param array $openid
     * @param array $tags
     * @param null $mp_info
     * @return bool|mixed
     * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
     * @throws \GuzzleHttp\Exception\GuzzleException
     * @throws \Psr\SimpleCache\InvalidArgumentException
     * Author: fudaoji<fdj@kuryun.cn>
     */
    public static function tagAdd($openid = [], $tags = [], $mp_info = null)
    {
        is_string($openid) && $openid = [$openid];
        if(is_string($tags)){
            $tags = explode(',', $tags);
        }
        $app = self::getApp($mp_info);
        foreach ($tags as $tag){
            $res = $app->user_tag->tagUsers($openid, $tag);
            if(!empty($res['errcode'])){
                return $res['errmsg'];
            }
        }
        return true;
    }

    /**
     * 批量为用户移除标签
     * @param array $openid
     * @param array $tags
     * @param null $mp_info
     * @return bool|mixed
     * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
     * @throws \GuzzleHttp\Exception\GuzzleException
     * @throws \Psr\SimpleCache\InvalidArgumentException
     * Author: fudaoji<fdj@kuryun.cn>
     */
    public static function tagRemove($openid = [], $tags = [], $mp_info = null)
    {
        is_string($openid) && $openid = [$openid];
        if(is_string($tags)){
            $tags = explode(',', $tags);
        }
        $app = self::getApp($mp_info);
        foreach ($tags as $tag){
            $res = $app->user_tag->untagUsers($openid, $tag);
            if(!empty($res['errcode'])){
                return $res['errmsg'];
            }
        }
        return true;
    }

    /**
     * 修改备注
     * @param $openid
     * @param $remark
     * @param $mp_info
     * @return bool|mixed
     * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
     * @throws \GuzzleHttp\Exception\GuzzleException
     * @throws \Psr\SimpleCache\InvalidArgumentException
     * Author: fudaoji<fdj@kuryun.cn>
     */
    public static function remark($openid, $remark, $mp_info)
    {
        $app = self::getApp($mp_info);
        $res = $app->user->remark($openid, $remark);
        if(!empty($res['errcode'])){
            return $res['errmsg'];
        }
        return true;
    }

    /**
     * 根据openid获取粉丝信息
     * @param null $openid
     * @param null $mp_info
     * @return array|\EasyWeChat\Kernel\Support\Collection|mixed|object|\Psr\Http\Message\ResponseInterface|string|\think\db\Query|\think\Model|null
     * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
     * @throws \Psr\SimpleCache\InvalidArgumentException
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * Author: fudaoji<fdj@kuryun.cn>
     */
    public static function getFollowInfo($openid = null, $mp_info = null){
        $request = request();
        is_null($mp_info) && $mp_info = $request->mpInfo;
        if(empty($request->followInfo) && !empty($openid) && $mp_info){
            if(! $follow = FollowM::where([
                ['company_id','=', $mp_info['company_id']],
                ['mp_id' ,'=', $mp_info['id']],
                ['openid','=', $openid]
            ])
                ->find()){
                if(is_string($follow = self::userGet($openid, $mp_info))){
                    $follow = [
                        'openid' => $openid,
                        'subscribe' => 1,
                        'subscribe_time' => time(),
                        'headimgurl' => '',
                        'nickname' => '微信粉丝'
                    ];
                }else{
                    isset($follow['tagid_list']) && $follow['tagid_list'] = json_encode($follow['tagid_list'], JSON_UNESCAPED_UNICODE);
                }
                $follow['company_id'] = $mp_info['company_id'];
                $follow['mp_id'] = $mp_info['id'];
                FollowM::create($follow);
            }
            $request->followInfo = $follow;
        }
        return $request->followInfo;
    }

    /**
     * @param null $openid
     * @param null $mp_info
     * @return array|\EasyWeChat\Kernel\Support\Collection|mixed|object|\Psr\Http\Message\ResponseInterface|string|\think\db\Query|\think\Model|null
     * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
     * @throws \Psr\SimpleCache\InvalidArgumentException
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * Author: fudaoji<fdj@kuryun.cn>
     */
    public static function getFollowByUnionid($openid = null, $mp_info = null){
        $request = request();
        is_null($mp_info) && $mp_info = $request->mpInfo;
        return FollowM::where([
            ['company_id','=', $mp_info['company_id']],
            ['mp_id' ,'=', $mp_info['id']],
            ['unionid','=', $openid]
        ])->find();
    }
}